package cn.wolfcode.interceptor;

import cn.wolfcode.domain.Result;
import cn.wolfcode.util.JWTUtil;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Slf4j
@Component
public class MyInterceptor  implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token=request.getHeader("token");
        log.info("acquire the token from browser{}",token);
        try {
            JWTUtil.parseToken(token);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            Result notLogin=Result.error("NOT_LOGIN");
            response.setContentType("application/json;charset=utf-8");
             String json=JSON.toJSONString(notLogin);
             response.getWriter().println(json);
             return false;
        }
    }
}
